package com.homework;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.nio.charset.Charset;

public class Test {

    public static void main(String[] args) {
        t4();
    }

    public static void t5(){

    }

    public static void t4(){

        Charset ascii = Charset.forName("ASCII");
        Channel channel = new NioServerSocketChannel();
        ByteBufAllocator allocator = channel.alloc();
        ByteBuf buf = allocator.buffer(1, 12);
        ByteBuf byteBuf1 = allocator.heapBuffer(1, 15);
        ByteBuf byteBuf2 = allocator.directBuffer(1, 20);
        byte[] b = new byte[12];
        b[0] = 65;
        b[1] = 66;
        b[3] = 97;
        b[4] = 98;
        b[5] = 99;
        ByteBuf byteBuf = buf.writeBytes(b);
        ByteBuf byteBuf3 = byteBuf1.writeBytes(b);
        ByteBuf byteBuf4 = byteBuf2.writeBytes(b);
        System.out.println(byteBuf.toString(ascii));
        System.out.println(byteBuf3.toString(ascii));
        System.out.println(byteBuf4.toString(ascii));
        System.out.println(ByteBufUtil.hexDump(buf));
        System.out.println(ByteBufUtil.equals(byteBuf3,byteBuf4));
        System.out.println(byteBuf3.refCnt());
        System.out.println(byteBuf4.refCnt());
        byteBuf3.release();
        byteBuf4.release();
        System.out.println(byteBuf3.refCnt());
        System.out.println(byteBuf4.refCnt());
    }

    public static void t3(){

        Charset utf8 = Charset.forName("ASCII");
        ByteBuf buf = Unpooled.copiedBuffer("oo这是大坏..蛋", utf8);
        System.out.println((char)buf.getByte(0));

        int readerIndex = buf.readerIndex();
        int writerIndex = buf.writerIndex();

        System.out.println("readableBytes : "+buf.readableBytes());
        System.out.println("writableBytes : "+buf.writableBytes());

        buf.setByte(0,(byte)'B');

        System.out.println((char)buf.getByte(0));

        assert readerIndex == buf.readerIndex();
        assert writerIndex == buf.writerIndex();
    }

    public static void t2(){
        Charset utf8 = Charset.forName("UTF-8");
        ByteBuf buf = Unpooled.copiedBuffer("Netty in Action rocks!", utf8);
        ByteBuf copy = buf.copy(0, 14);
        System.out.println(copy.toString(utf8));

        buf.setByte(0,(byte)'J');

        System.out.println(buf.getInt(0));

        System.out.println((char) buf.getByte(0));
        System.out.println((char) copy.getByte(0));

        assert buf.getByte(0) == copy.getByte(0);

    }

    public static void t1(){
        Charset utf8 = Charset.forName("UTF-8");
        ByteBuf buf = Unpooled.copiedBuffer("Netty in Action rocks!", utf8);

        ByteBuf sliced = buf.slice(0, 5);
        System.out.println(sliced.toString(utf8));

        ByteBuf buf1 = buf.setByte(7, (byte) 'o');
        System.out.println(buf1.toString(utf8));
        assert buf.getByte(0) == sliced.getByte(0);
    }
}
